package com.hetwen.remoteboot.dao;

import java.util.ArrayList;
import java.util.List;

import com.hetwen.remoteboot.db.DBOpenHelper;
import com.hetwen.remoteboot.model.MacAddr;
import com.hetwen.remoteboot.utils.StringUtils;

import android.content.ContentValues;
import android.content.Context;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteOpenHelper;

public class MacAddrDao {

	private SQLiteOpenHelper helper;
	private String table = DBOpenHelper.TBNAME;
	
	public MacAddrDao(Context context) {
		helper = DBOpenHelper.getInstance(context);
	}
	
	/**
	 * 增
	 */
	public boolean insert(MacAddr mac){
		if(mac == null){
			return false;
		}
		
		if(StringUtils.isBlank(mac.name) || StringUtils.isBlank(mac.mac)){
			return false;
		}
		
		SQLiteDatabase db = helper.getWritableDatabase();
		if(!db.isOpen()){
			return false;
		}
		
		ContentValues values = new ContentValues();
		values.put("name", mac.name);
		values.put("mac", mac.mac);
		
		long result = db.insert(table, null, values);
		db.close();
		if(result != -1){
			return true;
		}
		
		return false;
	}
	
	/**
	 * 删
	 */
	public boolean delete(int id){
		SQLiteDatabase db = helper.getWritableDatabase();
		if(!db.isOpen()){
			return false;
		}
		String _id = id+"";
		int i = db.delete(table, "id=?", new String[]{_id});
		db.close();
		if(i >= 1){
			return true;
		}
		return false;
	}
	
	/**
	 * 改
	 */
	public boolean update(MacAddr mac){
		if(mac == null){
			return false;
		}
		
		if(StringUtils.isBlank(mac.name) || StringUtils.isBlank(mac.mac)){
			return false;
		}
		
		SQLiteDatabase db = helper.getWritableDatabase();
		if(!db.isOpen()){
			return false;
		}
		
		ContentValues values = new ContentValues();
		values.put("name", mac.name);
		values.put("mac", mac.mac);
		String _id = mac.id+"";
		int i = db.update(table, values, "id=?", new String[]{_id});
		db.close();
		if(i == 1){
			return true;
		}
		return false;
	}
	
	/**
	 * 查
	 */
	public List<MacAddr> queryAll(){
		SQLiteDatabase db = helper.getWritableDatabase();
		if(!db.isOpen()){
			return null;
		}
		List<MacAddr> list = new ArrayList<MacAddr>();
		Cursor cursor = db.query(table, new String[]{"id","name","mac"}, null, null, null, null, null, null);
		while(cursor.moveToNext()){
			MacAddr mac = new MacAddr();
			mac.id = cursor.getInt(0);
			mac.name = cursor.getString(1);
			mac.mac = cursor.getString(2);
			list.add(mac);
		}
		cursor.close();
		db.close();
		return list;
	}
	
	/**
	 * @param name
	 */
	public MacAddr queryByName(String name){
		if(StringUtils.isBlank(name)){
			return null;
		}
		
		SQLiteDatabase db = helper.getWritableDatabase();
		if(!db.isOpen()){
			return null;
		}
		
		MacAddr macAddr  = null;
		Cursor cursor = db.query(table, new String[]{"id","name","mac"}, "name=?", new String[]{name}, null, null, null, null);
		if(cursor.moveToFirst()){
			macAddr = new MacAddr();
			macAddr.id = cursor.getInt(0);
			macAddr.name = cursor.getString(1);
			macAddr.mac = cursor.getString(2);
		}
		cursor.close();
		db.close();
		return macAddr;
	}
	
	/**
	 * @param mac
	 */
	public MacAddr queryByMac(String mac){
		if(StringUtils.isBlank(mac)){
			return null;
		}
		
		SQLiteDatabase db = helper.getWritableDatabase();
		if(!db.isOpen()){
			return null;
		}
		
		MacAddr macAddr  = null;
		Cursor cursor = db.query(table, new String[]{"id","name","mac"}, "mac=?", new String[]{mac}, null, null, null, null);
		if(cursor.moveToFirst()){
			macAddr = new MacAddr();
			macAddr.id = cursor.getInt(0);
			macAddr.name = cursor.getString(1);
			macAddr.mac = cursor.getString(2);
		}
		cursor.close();
		db.close();
		return macAddr;
	}
	
	/**
	 * @return
	 */
	public boolean isNameExists(String name){
		MacAddr macAddr = queryByName(name);
		if(macAddr== null){
			return false;
		}else{
			return true;
		}
	}
	
	public boolean isMacExists(String mac){
		MacAddr macAddr = queryByMac(mac);
		if(macAddr== null){
			return false;
		}else{
			return true;
		}
	}
	
}
